package res

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

func response(c *gin.Context, code int, msg string, data any) {
	c.JSON(http.StatusOK, Response{Code: code, Msg: msg, Data: data})
}

func Ok(c *gin.Context, msg string, data any) {
	response(c, 0, msg, data)
}
func OkWithMsg(c *gin.Context, msg string) {
	Ok(c, msg, gin.H{})
}
func OkWithData(c *gin.Context, data any) {
	Ok(c, "", data)
}

var codeMap = map[int]string{
	1001: "用户不存在",
	1002: "密码错误",
	2001: "ip不合法",
	3001: "参数非法",
}

func Fail(c *gin.Context, msg string, data any) {
	response(c, -1, msg, data)
}
func FailWithMsg(c *gin.Context, msg string) {
	Fail(c, msg, gin.H{})
}
func FailWithCode(c *gin.Context, code int) {
	m, ok := codeMap[code]
	if !ok {
		m = "未知错误"
	}
	response(c, code, m, gin.H{})
}
